package com.wuhaha.a1911_demo.ui.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;
import androidx.databinding.library.baseAdapters.BR;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.wuhaha.a1911_demo.databinding.LayoutFeedTypeImageBinding;
import com.wuhaha.a1911_demo.databinding.LayoutFeedTypeVideoBinding;
import com.wuhaha.a1911_demo.model.Feed;
import com.wuhaha.a1911_demo.view.ListPlayerView;

import org.jetbrains.annotations.NotNull;

public abstract class FeedApater extends PagedListAdapter<Feed,FeedApater.ViewHolder> {

    private LayoutInflater from;
    private Context context;
    private String category;



    protected FeedApater(Context context, String category) {
        super(new DiffUtil.ItemCallback<Feed>() {
            @Override
            public boolean areItemsTheSame(@NonNull @NotNull Feed oldItem, @NonNull @NotNull Feed newItem) {
                return oldItem.id == newItem.id;
            }

            @Override
            public boolean areContentsTheSame(@NonNull @NotNull Feed oldItem, @NonNull @NotNull Feed newItem) {
                return false;
            }
        });
        from = LayoutInflater.from(context);
        this.context = context;
        this.category = category;
    }

    @Override
    public int getItemViewType(int position) {
        Feed feed = getItem(position);
        return feed.itemType;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = null;
        if (viewType == Feed.TYPE_IMAGE_TEXT){
            binding = LayoutFeedTypeImageBinding.inflate(from);
        }else{
            binding = LayoutFeedTypeVideoBinding.inflate(from);
        }
        return new ViewHolder(binding.getRoot(),binding);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        holder.bindDate(getItem(position));
    }

    public abstract void onViewAttachedToWindow2(@NonNull ViewHolder holder);

    public abstract void onViewDetachedFromWindow2(@NonNull ViewHolder holder);

    public abstract void onStartFeedDetailActivity(Feed feed);

    public class ViewHolder extends RecyclerView.ViewHolder {

         ViewDataBinding binding;
        public ListPlayerView listPlayerView;
        public ImageView feedImage;

        public ViewHolder(@NonNull @NotNull View itemView, ViewDataBinding binding) {
            super(itemView);
            this.binding = binding;
        }

        public void bindDate(Feed item) {
            binding.setVariable(BR.lifeCycleOwner, context);
            if (binding instanceof LayoutFeedTypeImageBinding){
                LayoutFeedTypeImageBinding imageBinding = (LayoutFeedTypeImageBinding) binding;
                imageBinding.setFeed(item);
                imageBinding.feedImage.bindData(item.width,item.height,16,item.cover);

            }else{
                LayoutFeedTypeVideoBinding videoBinding = (LayoutFeedTypeVideoBinding) binding;
                videoBinding.setFeed(item);
                listPlayerView=videoBinding.listPlayerView;
                videoBinding.listPlayerView.bindData(category,item.width,item.height,item.cover,item.url);

            }
        }

        public boolean isVideoItem() {
            return binding instanceof LayoutFeedTypeVideoBinding;
        }

        public ListPlayerView getListPlayerView() {
            return listPlayerView;
        }
    }
}
